/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/*
 * @Author: your name
 * @Date: 2020-08-07 09:29:37
 * @LastEditTime: 2020-08-14 09:19:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \kuaidihelp\kuaidihelp_shop_admin\mock\express.js
 */
import { getDispatchData, getOrderQuery, getOrderDetail, feeClose } from "@/services/express";
import { message } from "antd";

export default {
  namespace: "submissionRecords",

  state: {
    recordList: {
      //上交记录列表
      list: [],
      pagination: {},
    },
    siteList: {
      //上交的站点列表
      list: [],
      pagination: {},
    },
    orderDetail: {}, //订单详情
    downLoadURL: null,
  },
  effects: {
    //获取上交的站点列表
    *getDispatchData({ payload, then = () => {} }, { call, put }) {
      const response = yield call(getDispatchData, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { list, total, page } = data;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            siteList: {
              list,
              pagination: {
                total,
                current: page,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        then(response);
      } else {
        message.error(msg);
      }
    },
    //上交记录列表
    *getOrderQuery({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOrderQuery, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { count, list, page, size, path } = data;
      const downLoadURL = path && null;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            recordList: {
              list,
              pagination: {
                total: count,
                current: page,
                pageSize: Number(size) || 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
            downLoadURL,
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: "save",
          payload: {
            downLoadURL: null,
          },
        });
        __dva_reject(response);
        message.error(msg);
      }
    },

    *getOrderDetail({ payload, then = () => {} }, { call, put }) {
      const response = yield call(getOrderDetail, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            orderDetail: data,
          },
        });
        then(response);
      } else {
        message.error(msg);
      }
    },

    *feeClose({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(feeClose, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject(response);
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
